#include "FileDownRequest.h"


FileDownRequest::FileDownRequest(void)
{
	m_pkDownloadThread = new DownloadThread();
}


FileDownRequest::~FileDownRequest(void)
{
}

void FileDownRequest::SendRETR(void)
{
	send(m_nConn, "PWD\r\n", 5, 0);
	m_nState = m_kFileDownenum::PWD;
}

int FileDownRequest::ResponseAnalysis()
{
	//PWD > PASV > RETR > data connection
	m_pcCopyParse = m_cCopyBuf;

	switch (*m_pcCopyParse)
	{
	case '2':
		if (m_nState == m_kFileDownenum::PWD)
		{
			ResponsePWD();
			send(m_nConn, "PASV\r\n", 6, 0);
			m_nState = m_kFileDownenum::PASV;
		}
		else if (m_nState == m_kFileDownenum::PASV)
		{
			SetDataPort();
			CreateDataConn();
			sprintf(m_cSendBuf, "RETR %s%s\r\n", m_cServPath, m_cFileName);
			send(m_nConn, m_cSendBuf, strlen(m_cSendBuf), 0);
			m_nState = m_kFileDownenum::RETR;
			delete m_pkDownloadThread;
			m_pkDownloadThread = new DownloadThread();
			m_pkDownloadThread->SetDataSock(m_nDataConn);
			m_pkDownloadThread->SetFilePointer(m_pFile);
			m_pkDownloadThread->SetProgressBar(m_pkProgressCtrl, m_nFileSize);
			m_pkDownloadThread->Run();
			m_nState = m_kFileDownenum::NONE;
		}
		break;

	case '4':
		//make Data connection failed handle
		m_nState = m_kFileDownenum::NONE;
		AfxMessageBox(m_cCopyBuf);
		break;

	case '5':
		//make Data connection failed handle
		m_nState = m_kFileDownenum::NONE;
		AfxMessageBox(m_cCopyBuf);
		break;
	}
	return 0;
}

void FileDownRequest::SetProgressBar(CProgressCtrl *pProCtrl, int nFileSize)
{
	m_pkProgressCtrl = pProCtrl;
	m_nFileSize = nFileSize;
}
